package com.burgos.inventory;

import com.burgos.integration.IntentIntegrator;
import com.burgos.integration.IntentResult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainMenu extends Activity implements OnClickListener {

	private Button searchB, BarCodeB, statisticsB;
	Intent i;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);
		searchB = (Button) findViewById(R.id.searchproduct);
		BarCodeB = (Button) findViewById(R.id.byscan);
		statisticsB = (Button) findViewById(R.id.viewstatistics);
		
		searchB.setOnClickListener(this);
		BarCodeB.setOnClickListener(this);
		statisticsB.setOnClickListener(this);

	}
	
	@Override
	public void onClick(View v) {
		//TODO execute method to login
		
		switch (v.getId()) {
		case R.id.searchproduct:
			i = new Intent(this, SearchMenu.class);
			startActivity(i);
			break;
		case R.id.byscan:
			//launch barcode reader app and get string
			IntentIntegrator scanIntegrator = new IntentIntegrator(this);
			scanIntegrator.initiateScan();
			break;
		case R.id.viewstatistics:
			i = new Intent(this, Statistics.class);
			startActivity(i);
			break;

		default:
			break;
		}
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (scanningResult != null) {
			String scanContent = scanningResult.getContents();
			String scanFormat = scanningResult.getFormatName();
			Intent i = new Intent(MainMenu.this, BarScanner.class);
			i.putExtra("keyword",scanContent);
			i.putExtra("format",scanFormat);
			finish();
			startActivity(i);
			}
		else{
		    Toast toast = Toast.makeText(getApplicationContext(),
		        "No scan data received!", Toast.LENGTH_SHORT);
		    toast.show();
		}
	}
}
